package com.zhupf.dynamic;

/**
 * @author zhupf
 * @date 2024年03月04日 16:39
 * @Description
 *
 *  198. 打家劫舍
 */
public class Rob {

    public static void main(String[] args) {
        int[] nums = {1,2,3,1};
        System.out.println(rob(nums));
    }

    public static int rob(int[] nums) {
        if(nums.length == 1){
            return nums[0];
        }
        if(nums.length == 2){
            return Math.max(nums[0],nums[1]);
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = nums[1];
        dp[2] = nums[2]+nums[0];
        for (int i = 3; i<dp.length;i++){
            dp[i] = Math.max(dp[i-2],dp[i-3])+nums[i];
        }
        return Math.max(dp[dp.length-1],dp[dp.length-2]);
    }

}
